const puppeteer = require("puppeteer");
const cheerio = require("cheerio");

async function music(){
    const browser = await puppeteer.launch({
        defaultViewport:null
    })
    const page = await browser.newPage()
    await page.goto("https://music.163.com/");
    // 1.先找到iframe
    var iframe = await page.$("#g_iframe")
    // 2.contentFrame 去获取iframe中的html结构
    var frame = await iframe.contentFrame();
    // 3.抓取
    var content = await frame.content()
    const $ = cheerio.load(content,{
        decodeEntities:false
    })
    var music={}
    var banner = $(".ban-img img").attr("src")
    var hot = []
    var items = $(".n-rcmd .m-cvrlst li")
    items.each((index,value)=>{
        var id = $(value).find(".u-cover a").attr("data-res-id")
        var imgUrl = $(value).find(".u-cover img").attr("src")
        var name = $(value).find(".u-cover a").attr("title")
        var playCount = $(value).find(".bottom .nb").text()
        hot.push({
            id,
            imgUrl,
            name,
            playCount
        })
    })
    var newCd = []
    var item = $(".roll .f-cb li")
    item.each((index,value)=>{
        var img = $(value).find(".j-img").attr("data-src")
        var title = $(value).find(".f-thide a").html()
        var name = $(value).find(".s-fc3").html()
        newCd.push({
            img,
            title,
            name
        })
    })
    music = {banner,hot,newCd}
    return music
}
module.exports = music